package com.cx.mq.rabbitmq.config;

/**

 */

import com.cx.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 *
 * ConfirmCallback在到达交换机前这个阶段起作用
 * 发送消息的确认机制原则::
 *  (1)如果消息没有到exchange,则confirm回调,ack=false
 *  (2)如果消息到达exchange,则confirm回调,ack=true
 *
 * Author: chenxiaoyi
 * Date: 2020/8/8 16:07
 */
@Slf4j
@Component
public class ConfirmCallBackImpl implements RabbitTemplate.ConfirmCallback {
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(!ack){
            System.out.println(JsonUtils.object2String(correlationData));
            log.info("消息未到达交换机,原因:{}",cause);
        }
    }
}
